﻿
#include <QDebug>

#include "websocket/HttpClientConnection.h"
#include "websocket/LiveManager.h"
#include <assert.h>
#include "base/TraceLog.h"



HttpClientConnection::HttpClientConnection(QWebSocket* socket,WebSocketManager* manager,LiveManager * _live_manager,QObject *parent)
            : QObject(parent)
            ,_socket(socket)
            , m_pCmdRouter(NULL)
            , one_live_manager(_live_manager)
            ,_manager(manager)
{
    LogDEBUG("%s",__FUNCTION__);
    client_address=_socket->peerAddress();
    connect(_socket, &QWebSocket::textMessageReceived, this, &HttpClientConnection::processTextMessage);
    connect(_socket, &QWebSocket::binaryMessageReceived, this, &HttpClientConnection::processBinaryMessage);
    connect(_socket, &QWebSocket::disconnected, this, &HttpClientConnection::disconnected);
}
 int HttpClientConnection::Init()
 {
    m_pCmdRouter = new CmdRouter(this);
    assert(m_pCmdRouter != NULL);
    return 0;
 }
HttpClientConnection::~HttpClientConnection()
{
    LogDEBUG("%s",__FUNCTION__);
    if(m_pCmdRouter)
    {
        delete m_pCmdRouter;
        m_pCmdRouter = NULL;
    }
    LogDEBUG("destroy Client %s",client_address.toString().toStdString().c_str());

    if(_socket)
    {
        _socket->deleteLater();
        _socket = NULL;
    }
}


void HttpClientConnection::processTextMessage(QString message)
{
       QJsonParseError error;
       QJsonDocument jsonDocument = QJsonDocument::fromJson(message.toUtf8(), &error);
       if (error.error == QJsonParseError::NoError)
       {
           if (!(jsonDocument.isNull() || jsonDocument.isEmpty())) {
               if (jsonDocument.isObject()) {
                   QString strOut;
                   QJsonObject jsonObj = jsonDocument.object();
                   QString strMethod = jsonObj.take("method").toString();
                   LogDEBUG("%s:strMethod is %s",__FUNCTION__,strMethod.toStdString().c_str());
                   m_pCmdRouter->HandleJsonMethodCall(strMethod,jsonDocument,strOut);
               } else if (jsonDocument.isArray()) {
                   // ...
               }
           }
       }
       else
       {

           //qDebug("wrong json format:\n");
           //qDebug() << "wrong json formate:" << message;
           LogDEBUG("%s:wrong json formate: %s",__FUNCTION__,message.toStdString().c_str());
       }

}


int HttpClientConnection::sendJson(QString str)
{
    if(_socket)
    {
        return _socket->sendTextMessage(str);
    }

    return 0;
}

int HttpClientConnection::SendCmd(QString strCmd,QString strParams)
{
       QJsonObject json;
       json.insert("jsonrpc", "2.0");
       json.insert("method", strCmd);
       json.insert("params", strParams);
       QJsonDocument document;
       document.setObject(json);
       QByteArray byte_array = document.toJson(QJsonDocument::Compact);
       sendJson(QString (byte_array));

    return 0;
}

void HttpClientConnection::processBinaryMessage(QByteArray message)
{
    if (_socket) {
        _socket->sendBinaryMessage(message);
    }
}

QHostAddress HttpClientConnection::get_client_address() {
    return client_address;
}

int  HttpClientConnection::DoStartPullStream(QString & pull_url,int num)
{

    if(one_live_manager)
    {
        return one_live_manager->DoStartPullStream(pull_url, num);
    }

    return 0;
}
int  HttpClientConnection::DoStopPullStream(QString & pull_url)
{
    if(one_live_manager)
    {
        return one_live_manager->DoStopPullStream(pull_url);
    }

    return 0;
}




